#include"preCmpH.h"
#include"UIManager.h"

BEGIN_NAMESPACE(ui_manager)
//=====================================
// Constructors, Destructor, Operators
//=====================================

UIManager::UIManager(){}
UIManager::~UIManager(){}
//=====================================
// Functionality
//=====================================

void UIManager::Bind(ui_controls::UIControl* toBind)
{
	if(!toBind)
	{
		UIException e("Parameter not initialized",
			"UIManager.cpp",
			"UIManager",
			"Bind");
		throw e;
	}
	toBind->BoundBy(*this, false);
}
void UIManager::Unbind( std::string control_name )
{
	root_.getChild(control_name)->UnboundBy(*this);
}
// UNFINISHED
void UIManager::TestInvariant() const
{
}
void UIManager::ProcessEvents()
{
	while(!eventManger_->isEmpty()){
		root_.PostEvent(eventManger_->getEvent());
	}
}
void UIManager::Clear()
{
	root_.Clear();
}
//=====================================
// Getters
//=====================================

UIManager& UIManager::getInstance()
{
	static UIManager* instance = null;
	if(instance == null)
	{
		instance = new UIManager();
	}
	return *instance;
}
ui_graphics::UIRenderer& UIManager::getUIRenderer()
{
	return *renderer_;
}
ui_fonts::UIFontManager& UIManager::getUIFontManager()
{
	return *fontManager_;
}

void UIManager::ShutDown()
{
	renderer_->ShutDown();
	fontManager_->ShutDown();
	eventManger_->ShutDown();
	delete renderer_;
	delete fontManager_;
	delete eventManger_;
}

void UIManager::StartUp()
{
	renderer_ = &ui_graphics::UIRenderer::getInstance();
	fontManager_ = &ui_fonts::UIFontManager::getInstance();
	eventManger_ = &ui_events::UIEventManager::getInstance();
	root_.focused_ = true;
	root_.focus_ = &root_;
	renderer_->StartUp(*this);
	fontManager_->StartUp(*this);
	eventManger_->StartUp(*this);
}

ui_events::UIEventManager& UIManager::getUIEventManager()
{
	return *eventManger_;
}

ui_controls::UIControlContainer& UIManager::getRoot()
{
	return root_;
}

END_NAMESPACE